package io.sqrtqiezi.spark.dataframe

import org.apache.spark.sql.SparkSession

object PivotOperator {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local")
      .appName("pivot practise")
      .getOrCreate()

    val df = spark.read.format("csv")
      .option("header", "true")
      .option("inferSchema", "true")
      .load("data/retail-data/all/*.csv")
      .coalesce(5)

    df.createOrReplaceTempView("retail")

    df.printSchema

    spark.sql(
      """
        |select distinct country from retail""".stripMargin)
      .show()

    spark.sql(
      """
        |select * from retail
        |pivot (
        |  sum(quantity) as a , max(unitprice) as b
        |  for country in ('Germany' as Germany, 'Hong Kong' as hongkong)
        |)
        |where Germany_a is not null or hongkong_a is not null""".stripMargin)
      .show
  }
}
